package com.bite.springmvc.controller;

import com.bite.springmvc.model.UserInfo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//@RestController //返回数据 = @Controller + @ResponseBody(返回数据）
@RequestMapping("/response")
@Controller //返回视图
public class ResponseController {
    //返回视图
    @RequestMapping("/index")
    public String index() {
        return "/index.html";//不加/访问的是相对路径，也就是/response/index.html;加了/访问路径直接就是index.html
    }

    //返回数据
    @ResponseBody
    @RequestMapping("/returnData")
    public String returnData() {
        return "返回数据";
    }

    @ResponseBody
    @RequestMapping("/returnHTML")
    public String returnHTML() {
        return "<h1>我是HTML页面</h1>";
    }

    @ResponseBody
    @RequestMapping("/returnJSON")
    public UserInfo returnJSON() {
        UserInfo userInfo = new UserInfo("zhangsan",18,"1234");
        return userInfo;
    }

    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response) {
        response.setStatus(401);
        return "设置状态码成功";//状态码设置成功但是显示并不会报错
    }


    @ResponseBody
    @RequestMapping(value = "r1", produces = "application/json;charset=utf-8")//指定返回数据格式
    public String r1(){
        return "{\"status\":400}";
    }

    @ResponseBody
    @RequestMapping("setHeader")
    public String setHeader(HttpServletResponse response) {
        response.setHeader("MyHeader","MyHeaderValue");
        return "hahaha";
    }
}
